package org.goyo.main.services.base.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.base.ISupplierDao;
import org.goyo.main.domain.entity.BaseObject;
import org.goyo.main.domain.entity.base.Supplier;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.base.ISupplierService;
import org.goyo.main.web.controller.system.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class SupplierServiceImpl extends BaseService implements ISupplierService{
  protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private ISupplierDao supplierDao;

  @Override
  public BaseObject saveOrUpdateInfo(Supplier info) throws DBException, ServiceException {
    String uuid = null;
    String date = DateUtils.parseDate("yyyy-MM-dd", new Date());
    SysUser user = (SysUser)SessionUtil.getAttribute("user");
    //保存用户数据
    if(info.getUuid() == null || "".equals(info.getUuid())){
        info.setUuid(null);
        info.setStatus(Constants.SAVESTATUS);
        info.setCreateDate(date);
        info.setCreateUser(user);
        info.setAlterDate(date);
        info.setAlterUser(user);
//		alterBillNumber(Supplier.BIZTYPE, info.getNumber());
        uuid = supplierDao.save(info);
    }else{
        info.setAlterDate(date);
        info.setAlterUser(user);
        supplierDao.update(info);
        uuid = info.getUuid();
    }
    
    BaseObject obj = new  BaseObject();
    obj.setCreateDate(date);
    obj.setCreateUser(user);
    obj.setAlterDate(date);
    obj.setAlterUser(user);
    obj.setUuid(uuid);
    obj.setStatus(Constants.SAVESTATUS);
    return obj;
}

  @Override
  public void delByUuids(Object[] uuids) throws DBException, ServiceException {
    for(Object uuid : uuids){
      Supplier supplier  = supplierDao.get(Supplier.class, uuid.toString());
      supplierDao.delete(supplier);
  }
}

  @Override
  public Supplier findByUuid(String uuid) throws DBException, ServiceException {
    return supplierDao.get(Supplier.class, uuid);
    }

  @Override
  public Pager<Supplier> findByCondition(String where, Pager pager) throws DBException,
      ServiceException {
    return  supplierDao.findByCond(where, pager);
  }

  @Override
  public boolean checkNumberUsed(String uuid,String number) throws DBException, ServiceException {
    int i =0;
    try {
      String hql = "";
      if(StringUtils.isEmpty(uuid)){
        hql = "select count(*) from Supplier where number ='"+number+"'";
      }else{
        hql = "select count(*) from Supplier where uuid !='"+uuid+"' and number ='"+number+"'";
      }
      List list =  supplierDao.findObjects(hql, null, null);
      if(null!=list && list.size()>0 && (Long)list.get(0) >0){
       return true;
      }else{
        return false;
      }
    } catch (Exception e) {
      logger.error(e.getMessage(),e);
      return false;
    }
}
}
